<template>
  <el-dialog
    v-model="visible"
    :title="!dataForm.currentId ? '新增' : '修改'"
    :close-on-click-modal="false"
  >
    <el-form
      ref="dataFormRef"
      :model="dataForm"
      :rules="dataRule"
      label-width="80px"
      @keyup.enter="onSubmit()"
    >
      <el-form-item
        label="图片"
        prop="imgUrl"
      >
        <pic-upload v-model="dataForm.imgUrl" />
      </el-form-item>
      <el-form-item
        label="类型"
        prop="type"
      >
        <el-input
          v-model="dataForm.type"
          controls-position="right"
          :min="0"
          label="类型"
        />
      </el-form-item>

      <el-form-item
        label="外部链接"
        prop="url"
      >
        <el-input
          v-model="dataForm.url"
          controls-position="right"
          :min="0"
          label="外部链接"
        />
      </el-form-item>

      <el-form-item
        label="排序号"
        prop="seq"
      >
        <el-input-number
          v-model="dataForm.sortOrder"
          controls-position="right"
          :min="0"
          label="排序号"
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="visible = false">
          取消
        </el-button>
        <el-button
          type="primary"
          @click="onSubmit()"
        >
          确定
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup>
import { ElMessage } from 'element-plus'
import { Debounce } from '@/utils/debounce'
const emit = defineEmits(['refreshDataList'])
const visible = ref(false)
const contentEnRef = ref(null)
const resourcesUrl = import.meta.env.VITE_APP_RESOURCES_URL
// eslint-disable-next-line no-unused-vars
const validateTitle = (rule, value, callback) => {
  if (!value.trim()) {
    dataForm.value.url = ''
    callback(new Error('外部链接不能为空'))
  } else {
    callback()
  }
}
const dataRule = {
  url: [
    { required: true, message: '外部链接不能为空', trigger: 'blur' },
    { validator: validateTitle, trigger: 'blur' }
  ]
}

const dataForm = ref({
  imgUrl: null
})
const dataFormRef = ref(null)
const init = (id) => {
  dataForm.value.id = id || 0
  visible.value = true
  nextTick(() => {
    dataFormRef.value?.resetFields()
    if (dataForm.value.id) {
      http({
        url: http.adornUrl('/admin/admin/cateImg/info/' + dataForm.value.id),
        method: 'get',
        params: http.adornParams()
      })
        .then(({ data }) => {
          dataForm.value = data
        })
    }
  })
}
defineExpose({ init })

/**
 * 表单提交
 */
const onSubmit = Debounce(() => {
  console.log(dataForm.value, 'dataForm.value')
  dataFormRef.value?.validate((valid) => {
    if (valid) {
      http({
        url: http.adornUrl('/admin/cateImg'),
        method: dataForm.value.id ? 'put' : 'post',
        data: http.adornData(dataForm.value)
      })
        .then(() => {
          ElMessage({
            message: '操作成功',
            type: 'success',
            duration: 1500,
            onClose: () => {
              visible.value = false
              emit('refreshDataList')
              dataForm.value.content = ''
            }
          })
        })
    }
  })
})

</script>
